﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Collections.Generic;
using Jzy.Workflow.Status.Code;
using System.Linq;

namespace Jzy.Workflow.Code
{
    public class Flow
    {
        private List<FlowLine> _lines;
        private List<FlowNode> _nodes;

        private Canvas _canvas;

        public int _flowID
        {
            get;
            set;
        }

        public Flow(int flowID, Canvas cavas)
        {
            _flowID = flowID;
            _canvas = cavas;
            _lines = new List<FlowLine>();
            _nodes = new List<FlowNode>();
        }


        public void Init(string taskID)
        {
            Jzy.Workflow.Status.DataService.FlowSoapClient flowData = DataFactory.Create();

            flowData.GetWorkFlowWithApprovedNodeCompleted += new EventHandler<Jzy.Workflow.Status.DataService.GetWorkFlowWithApprovedNodeCompletedEventArgs>(flowData_GetWorkFlowWithApprovedNodeCompleted);


            flowData.GetWorkFlowWithApprovedNodeAsync(_flowID, taskID, taskID);
           

            //FlowNode start = new FlowNode("开始(拟稿)");
            //start.Type = NodeType.开始;
            //FlowNode end = new FlowNode("结束");
            //end.Type = NodeType.结束;
            //FlowNode node1 = new FlowNode("部门经理审批");
           
            //FlowNode node2 = new FlowNode("节点2");
            //FlowNode node3 = new FlowNode("节点3");

            //FlowLine line1 = new FlowLine(start, node1);
            //FlowLine line2 = new FlowLine(node1, node2);
            //FlowLine line3 = new FlowLine(node2, node3);
            //FlowLine line4 = new FlowLine(node3, end);
            //FlowLine line5 = new FlowLine(node1, node3);
            //FlowLine line6 = new FlowLine(node1, end);

            //_nodes.Add(start);
            //_nodes.Add(node1);
            //_nodes.Add(node2);
            //_nodes.Add(node3);
            //_nodes.Add(end);

            //_lines.Add(line1);
            //_lines.Add(line2);
            //_lines.Add(line3);
            //_lines.Add(line4);
            //_lines.Add(line5);
            //_lines.Add(line6);

            //RenderFlow();
        }

        void flowData_GetWorkFlowWithApprovedNodeCompleted(object sender, Jzy.Workflow.Status.DataService.GetWorkFlowWithApprovedNodeCompletedEventArgs e)
        {
            string taskID = e.UserState as string;

            XElement xml = XElement.Parse(e.Result);
            IEnumerable<XElement> nodes = xml.Element("Nodes").Elements("Node");

            XElement approvedNodes = xml.Element("ApprovedNodes");
            if (approvedNodes != null)//审批过程中
            {
                var approvedTasks = approvedNodes.Elements().Select(Ele => new { TaskID = Ele.Element("TaskID").Value, NodeID = Ele.Element("NodeID").Value, State = int.Parse(Ele.Element("State").Value), SendTime = DateTime.Parse(Ele.Element("SendTime").Value) });
                foreach (XElement node in nodes)
                {
                    NodeApproveState state = NodeApproveState.未审批;
                    string nodeID = node.Element("ID").Value;

                    var tmp = approvedTasks.Where(T => T.NodeID == nodeID).OrderByDescending(T => T.SendTime);

                    if (tmp.Count() > 0)
                    {
                        var task = tmp.First();

                        if (task.State == (int)FlowNodeState.存草稿 || task.State == (int)FlowNodeState.待处理)
                        {
                            state = NodeApproveState.审批中;
                        }
                        else if (task.State == (int)FlowNodeState.已完成)
                        {
                            state = NodeApproveState.已审批;
                        }
                    }
                    _nodes.Add(FlowNode.Parse(node, state));
                }
            }
            else//新增表单
            {
                foreach (XElement node in nodes)
                {
                    NodeApproveState state = NodeApproveState.未审批;
                    _nodes.Add(FlowNode.Parse(node, state));
                }
            }

            IEnumerable<XElement> lines = xml.Elements("Lines").Elements("Line");
            foreach (XElement line in lines)
            {
                _lines.Add(FlowLine.Parse(line, _nodes));
            }

            RenderFlow();
        }

        private void RenderFlow()
        {
            _canvas.Children.Clear();
            for (int i = 0; i < _nodes.Count; i++)
            {
                _canvas.Children.Add(_nodes[i].Node);
            }

            for (int i = 0; i < _lines.Count; i++)
            {
                _canvas.Children.Add(_lines[i].Line);
                _lines[i].Draw();
            }
        }
    }
}
